package com.health.utils;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @Classname User
 * @Description TODO
 * @Version 1.0.0
 * @Date 2023/3/17 21:45
 * @Author RainGrd
 */
//创建会话，获取当前登录对象
@Component
public class UserUtils {

    /**
     * 判断此用户中是否包含roleName菜单权限
     *
     * @param roleName
     * @return
     */
    public Boolean hasRole(String roleName) {
        //获取UserDetails类，
        UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        List<String> roleCodes = new ArrayList<>();
        for (GrantedAuthority authority : userDetails.getAuthorities()) {
            System.out.println(authority);
            //getAuthority()返回用户对应的菜单权限
            roleCodes.add(authority.getAuthority());
        }
        System.out.println(roleCodes);
        return roleCodes.contains(roleName);
    }

}
